package pnu.ssm.question;

import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.content.Context;
import android.content.res.AssetManager;

public class QuestionManager implements QuestionRequestable{
	private static final String TAG = "QuestionManager";
	
	public static final int MAX_QUESTION = 1;
	private static final String FILE_NAME = "problem_grade_1.xml";
	
	private AssetManager assets;
	private Context context;

	public QuestionManager(Context context) {
		this.context = context;
		assets = context.getResources().getAssets();
	}

	private Question getQuestion(int num){

		try {/**/
			String path = "problemData/" + FILE_NAME;
			InputStream is = assets.open(path);

			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(is);

			doc.getDocumentElement().normalize();

			//System.out.println("Root element "+ doc.getDocumentElement().getNodeName());
			NodeList nodeLst = doc.getElementsByTagName("problem");
			//System.out.println(doc.getDocumentElement().getNodeName()+"'s problem");

			for (int s = 0; s < nodeLst.getLength(); s++) {

				Node fstNode = nodeLst.item(s);
				if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
					Element fstElmnt = (Element) fstNode;

					NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("number");
					Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
					NodeList fstNm = fstNmElmnt.getChildNodes();         
					//System.out.println("question : "+ ((Node) fstNm.item(0)).getNodeValue());

					NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("question");
					Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
					NodeList lstNm = lstNmElmnt.getChildNodes();     
					//System.out.println("question : "+ ((Node) lstNm.item(0)).getNodeValue());

					NodeList lstNmElmntLst1 = fstElmnt.getElementsByTagName("one");
					Element lstNmElmnt1 = (Element) lstNmElmntLst1.item(0);
					NodeList lstNm1 = lstNmElmnt1.getChildNodes();
					//System.out.println("one : "+ ((Node) lstNm1.item(0)).getNodeValue());

					NodeList lstNmElmntLst2 = fstElmnt.getElementsByTagName("two");
					Element lstNmElmnt2 = (Element) lstNmElmntLst2.item(0);
					NodeList lstNm2 = lstNmElmnt2.getChildNodes();
					//System.out.println("two : "+ ((Node) lstNm2.item(0)).getNodeValue());

					NodeList lstNmElmntLst3 = fstElmnt.getElementsByTagName("three");
					Element lstNmElmnt3 = (Element) lstNmElmntLst3.item(0);
					NodeList lstNm3 = lstNmElmnt3.getChildNodes();
					//System.out.println("three : "+ ((Node) lstNm3.item(0)).getNodeValue());

					NodeList lstNmElmntLst4 = fstElmnt.getElementsByTagName("four");
					Element lstNmElmnt4 = (Element) lstNmElmntLst4.item(0);
					NodeList lstNm4 = lstNmElmnt4.getChildNodes();
					//System.out.println("four : "+ ((Node) lstNm4.item(0)).getNodeValue());

					NodeList lstNmElmntLst5 = fstElmnt.getElementsByTagName("five");
					Element lstNmElmnt5 = (Element) lstNmElmntLst5.item(0);
					NodeList lstNm5 = lstNmElmnt5.getChildNodes();
//					System.out.println("five : "+ ((Node) lstNm5.item(0)).getNodeValue());

					NodeList lstNmElmntLst6 = fstElmnt.getElementsByTagName("answer");
					Element lstNmElmnt6 = (Element) lstNmElmntLst6.item(0);
					NodeList lstNm6 = lstNmElmnt6.getChildNodes();
					//System.out.println("five : "+ ((Node) lstNm6.item(0)).getNodeValue());

					if(num == Integer.valueOf(fstNm.item(0).getNodeValue()))
					{
						//return new Integer( ((Node) lstNm.item(0)).getNodeValue() );
						Question q = new Question();
						q.setQuestionNum(Integer.valueOf(fstNm.item(0).getNodeValue()));
						q.setQuestion(lstNm.item(0).getNodeValue());

						String[] strs = new String[5];
						strs[0] = lstNm1.item(0).getNodeValue();
						strs[1] = lstNm2.item(0).getNodeValue();
						strs[2] = lstNm3.item(0).getNodeValue();
						strs[3] = lstNm4.item(0).getNodeValue();
						strs[4] = lstNm5.item(0).getNodeValue();

						/*q.setOne(((Node) lstNm1.item(0)).getNodeValue());
						q.setTwo(((Node) lstNm2.item(0)).getNodeValue());
						q.setThree(((Node) lstNm3.item(0)).getNodeValue());
						q.setFour(((Node) lstNm4.item(0)).getNodeValue());
						q.setFive(((Node) lstNm5.item(0)).getNodeValue());
						 */

						q.setArrAnswer(strs);
						q.setCorrectAnswerNum(Integer.valueOf(lstNm6.item(0).getNodeValue()));
						
						return q;
					}
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		Question q2 = new Question();
		return q2;
	}

	@Override
	public Question requestQuestion(int num) {
		return getQuestion(num);
	}
	
	@Override
	public Question requestRandomQuestion() {
		int num = (int)(Math.random() * (MAX_QUESTION - 1) + 1);
		return getQuestion(num);
	}
}
